package cn.hy.preparelesson.common.service;

import cn.hy.preparelesson.common.entity.PlFeedbackUserDetail;
import cn.hy.preparelesson.common.entity.PlFeedbackUserDetailExample;
import cn.hy.preparelesson.common.mapper.PlFeedbackUserDetailMapper;
import cn.hy.preparelesson.common.utils.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 反馈 - 用户 -关联
 */
@Service
public class PLFeedBackUserDetailService extends  BaseService {

    @Autowired
    private PlFeedbackUserDetailMapper plFeedbackUserDetailMapper;

    /**
     * 批量保存 用户 - 反馈信息
     * @param schoolId
     * @param opeUsedId
     * @param feedbackId
     * @param userIdList
     * @param now
     */
    @Transactional(rollbackFor = Exception.class)
    public void batchSavePFUD(String schoolId, String opeUsedId, String feedbackId, List<String> userIdList, Date now) {
        batchDeleteFBUD(schoolId, feedbackId);
        List<PlFeedbackUserDetail> list = new ArrayList<>();
        for (String userId : userIdList) {
            PlFeedbackUserDetail plFeedbackUserDetail = new PlFeedbackUserDetail();
            plFeedbackUserDetail.setId(IdUtils.newId());
            plFeedbackUserDetail.setFeedbackId(feedbackId);
            plFeedbackUserDetail.setCreateBy(opeUsedId);
            plFeedbackUserDetail.setSchoolId(schoolId);
            plFeedbackUserDetail.setUserId(userId);
            plFeedbackUserDetail.setCreateAt(now);
            list.add(plFeedbackUserDetail);
        }
        plFeedbackUserDetailMapper.batchSavePlLessonFeedBackUserDetail(list);
    }

    /**
     * 根据feedbackId 查询反馈-反馈人 关联关系集合
     * @param schoolId
     * @param feedbackId
     * @return
     */
    public List<PlFeedbackUserDetail> findListByFeedbackId(String schoolId, String feedbackId) {
        PlFeedbackUserDetailExample example = new PlFeedbackUserDetailExample();
        example.createCriteria().andSchoolIdEqualTo(schoolId).andFeedbackIdEqualTo(feedbackId);
      return   plFeedbackUserDetailMapper.selectByExample(example);
    }

    /**
     * 根据feedbackIdList 查询反馈-反馈人 关联关系集合
     * @param schoolId
     * @param feedbackIdList
     * @return
     */
    public List<PlFeedbackUserDetail> findListByFeedbackIdIn(String schoolId, List<String> feedbackIdList) {
        PlFeedbackUserDetailExample example = new PlFeedbackUserDetailExample();
        example.createCriteria().andSchoolIdEqualTo(schoolId).andFeedbackIdIn(feedbackIdList);
        return   plFeedbackUserDetailMapper.selectByExample(example);
    }

    /**
     * 根据userId, schoolId 查询反馈-反馈人 关联关系集合
     * @param schoolId
     * @param userId
     * @return
     */
    public List<PlFeedbackUserDetail> findListBySchoolIdAndUserId(String schoolId, String userId) {
        PlFeedbackUserDetailExample example = new PlFeedbackUserDetailExample();
        example.createCriteria().andSchoolIdEqualTo(schoolId).andUserIdEqualTo(userId);
        return   plFeedbackUserDetailMapper.selectByExample(example);
    }


    /**
     * 批量删除反馈 - 反馈给谁关联关系
     * @param schoolId
     * @param feedbackId
     */
    @Transactional(rollbackFor = Exception.class)
    public void batchDeleteFBUD(String schoolId, String feedbackId) {
        PlFeedbackUserDetailExample example = new PlFeedbackUserDetailExample();
        example.createCriteria().andSchoolIdEqualTo(schoolId).andFeedbackIdEqualTo(feedbackId);
        plFeedbackUserDetailMapper.deleteByExample(example);
    }

    /**
     *修改反馈 - 反馈给谁关联关系
     * @param schoolId
     * @param opeUserId
     * @param feedbackId
     * @param toUserIdList
     * @param date
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateFBLUDBy(String schoolId, String opeUserId, String feedbackId, List<String> toUserIdList, Date date) {
        batchDeleteFBUD(schoolId, feedbackId);
        if (!CollectionUtils.isEmpty(toUserIdList)) {
            batchSavePFUD(schoolId, opeUserId, feedbackId, toUserIdList, date);
        }
    }
}
